package core;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;

/**
 * 表单对话框
 * @author self
 *
 */
public class FormDialog extends Dialog<Boolean> {

	//表单项
	private Item[] items;
	
	/**
	 * 表单项
	 * @author self
	 *
	 */
	public static class Item{
		private String label;
		private Node input;
		public Item(String label,Node input) {
			this.label = label;
			this.input = input;
		}
		public String getLabel() {
			return label;
		}
		public Node getInput() {
			return input;
		}
	}
	
	public FormDialog(String title,Item... items) {
		this.items = items;
		this.setTitle(title);
		
		//设置表单面板
		Pane formPane = createFormPane();
		this.getDialogPane().setContent(formPane);
		
		//设置对话框按钮
		this.getDialogPane()
			.getButtonTypes()
			.addAll(ButtonType.FINISH,ButtonType.CANCEL);
		
		//设置对话框结果
		this.setResultConverter((buttonType)->{
			if(ButtonType.FINISH == buttonType) {
				return true;
			}
			return null;
		});
	}
	
	/**
	 * 创建表单面板
	 * @return　表单面板
	 */
	private Pane createFormPane() {
		GridPane gridPane = new GridPane();
		gridPane.setAlignment(Pos.CENTER);
		gridPane.setHgap(10);
		gridPane.setVgap(10);
		gridPane.setPadding(new Insets(25, 25, 25, 25));
		
		for(int i=0;i<items.length;i++) {
			Item item = items[i];
			Label label = new Label(item.getLabel());
			gridPane.add(label,0,i);
			gridPane.add(item.getInput(),1,i);
		}
		
		return gridPane;
	}
}
